<?php
class Solution {
	
public function __construct( &$alt_sql = false){
		global $sql;
		if ($alt_sql==false){
			$this->sql=$sql;
		} else {
			$this->sql=&$alt_sql;
		}	
	}
	

	/**
	 * Adds a new solution to the database or updates an existing one.
	 * Also creates a new result if one does not exist for this user for this test
	 *
	 * @return boolean
	 */
	public static function insert($sql, $sooritus, $vastus, $ulesanne, $kontrolltoo) {
		$testStatus = Test::checkStatus($sql, $kontrolltoo);
		if($testStatus){
			//global $sql;
			$query = "SELECT l.idlahendus FROM lahendus l WHERE l.sooritus ='".$sooritus."' AND
			l.ulesanne = '".$ulesanne."' LIMIT 1";
			
			$result = $sql->selectQuery($query);
			
			if( !count($result) == 0 ) {
				$query = "UPDATE lahendus l SET l.vastus ='".mysql_escape_string($vastus)."'
				 WHERE l.sooritus = '".$sooritus."' AND l.ulesanne = '".$ulesanne."'";
				
				$sql->query($query);
				if( $sql->affectedRows() == 1 ) {
						return $vastus;
					} else {
						$query = "SELECT l.vastus FROM lahendus l WHERE
						l.ulesanne = '".$ulesanne."' AND l.vastus = '".$vastus."'
						AND l.sooritus ='".$sooritus."'";
						if(count($sql->selectQuery($query))==1){
							return $vastus;
						}
					
					}	
				} else {
					
					$query = "INSERT INTO lahendus ( sooritus, vastus, ulesanne) 
						VALUES ( '".$sooritus."', '".mysql_escape_string($vastus)."', '".$ulesanne."')";
					
					$sql->query($query);
					if( $sql->affectedRows() == 1 ) {
						return $vastus;
					}	
				}
			
		} else return "Töö suletud";
		
		
		
	}
	
	//sets the feedback of a solution
	public static function setFeedback( $sql, $feedback, $points, $answerId ) {
		$answerId = $answerId;
		$feedback = $feedback;
		$points = $points; 
		
		//sends query
		$query = "UPDATE lahendus l SET l.tagasiside = '".$feedback."',
		 l.punktid ='".$points."' 
			WHERE l.idlahendus = '".$answerId."' LIMIT 1";
		
		$sql->query($query);
		
		if( !($sql->affectedRows() == 1) ) {
			//check if maybe the data that is being set is already set in the db 
			$query = "SELECT * FROM lahendus l WHERE l.tagasiside = '".$feedback."' AND
			 l.punktid ='".$points."' 
			AND l.idlahendus = '".$answerId."' LIMIT 1";
			$sql->query($query);
			if(count($sql->selectQuery($query))==1){
					return true;
					} else return false;
		} else return true;
		
	}
	
	//sets the received points of a solution
	public static function setPoints( $sql, $points, $questionId ) {
		$questionId = $questionId;
		$points = $points; 
		
		
		
		
		//sends query
		$query = "UPDATE lahendus l INNER JOIN 
			sooritus s ON l.sooritus = s.idsooritus 
			SET l.punktid ='".$points."' 
			WHERE l.ulesanne ='".$questionId."' AND s.idsooritus ='".$_SESSION['sooritus']."'";
		
		$sql->query($query);
		
		if( !($sql->affectedRows() == 1) ) {
			//check if maybe the data that is being set is already set in the db 
			$query = "SELECT * FROM lahendus l INNER JOIN 
			sooritus s ON l.sooritus = s.idsooritus 
			WHERE l.punktid ='".$points."' 
			AND l.ulesanne ='".$questionId."' AND 
			s.idsooritus ='".$_SESSION['sooritus']."'";
			$sql->query($query);
			if(count($sql->selectQuery($query))==1){
					return true;
					} else return false;
		} else return true;
		
	}
	
	
	
}